package bma.common.thrift.servicehub;

import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TProcessor;

import bma.common.langutil.core.ValueUtil;
import bma.common.langutil.http.QueryStringEncoder;

public class ThriftServiceBean {

	protected ThriftServiceNode node;
	protected String module;
	protected String protocol;
	protected TProcessor processor;
	protected Map<String, String> properties;

	public Map<String, String> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	public ThriftServiceNode getNode() {
		return node;
	}

	public void setNode(ThriftServiceNode node) {
		this.node = node;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public TProcessor getProcessor() {
		return processor;
	}

	public void setProcessor(TProcessor processor) {
		this.processor = processor;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getEntry() {
		String url = node.makeUrl();

		QueryStringEncoder qs = new QueryStringEncoder("");
		if (ValueUtil.notEmpty(module)) {
			qs.addParam("module", module);
		}
		if (ValueUtil.notEmpty(protocol)) {
			qs.addParam("protocol", protocol);
		}
		if (node.getFrameLength() > 0) {
			qs.addParam("frameLength", Integer.toString(node.getFrameLength()));
		}

		if (qs.isEmptyParam()) {
			return url;
		}
		StringBuilder sb = new StringBuilder(128);
		sb.append(url);
		if (url.indexOf('?') == -1) {
			sb.append('?');
		}
		qs.buildParams(sb);
		return sb.toString();
	}

	public Map<String, String> getServiceProperties() {
		return properties == null ? null : new HashMap<String, String>(
				properties);
	}
}
